package com.share.lottery.utils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.share.lottery.view.CustomToast;


import android.content.Context;
import android.widget.Toast;


public class JsonHelper {

	private Context context;
	private JSONObject jsonObject;
	
	private JSONObject dataObject;
	private String data;
	
	public JsonHelper(){
		
	}

	public JsonHelper(Context context, JSONObject jsonObject) {
		this.jsonObject = jsonObject;
		this.context = context;
		try {
			if(!jsonObject.isNull("data")){
				data= jsonObject.getString("data");
				dataObject=new JSONObject(data);
				setData(data);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

//	public JsonHelper(JSONObject jsonObject) {
//		super();
//		this.jsonObject = jsonObject;
//	}

	// public JsonHelper(Context context, JSONObject jsonObject) {
	// super();
	// this.context = context;
	// this.jsonObject = jsonObject;
	// }
	
	public String getString(JSONObject jsonObject,String name) {
		String rtnStr = "";
		try{
			rtnStr = jsonObject.getString(name).replaceAll("&#39;","\"").replaceAll("&#8216", "\\\\");
		}catch (Exception e) {
			rtnStr = "";
		}
		return rtnStr;
	}

	/**
	 * 获取json对应的key的value值
	 * 
	 * @param 需要获取的key
	 * @return
	 */
	public String getString(String name) {
		String rtnStr = "";
		try {
			if(!jsonObject.isNull("status")){
				String status = jsonObject.getString("status");
				if (status != null) {
					if ("0".equals(status))
						CustomToast.makeText(context, data, Toast.LENGTH_SHORT);
				}
				rtnStr = dataObject.getString(name).replaceAll("&#39;", "\"").replaceAll("&#8216", "\\\\");
			}else{
				rtnStr = jsonObject.getString(name).replaceAll("&#39;", "\"").replaceAll("&#8216", "\\\\");
			}
//			 JSONObject dataJson=new JSONObject(data);
//				jsonObject = new JSONObject(data);  .replaceAll("&#39;", "\"").replaceAll("&#8216", "\\\\")
		} catch (Exception e) {
			rtnStr = "";
		}
		return rtnStr;
	}



@SuppressWarnings("unchecked")
public List getPojosFromJSONArray(Class pojoClass, String json){
	if (json == null || json.equals("")) {
		return null;
	}
	ArrayList<Object> formList = new ArrayList<Object>();
	try {
		JSONArray jsonArray = new JSONArray(json);
		for (int i = 0; i < jsonArray.length(); i++) {
			JSONObject jsonObject = jsonArray.getJSONObject(i);
			Iterator<?> it = jsonObject.keys();  
            Object pojo = (Object) pojoClass.newInstance();
            while(it.hasNext()){//遍历JSONObject  
            	String key = (String) it.next().toString();  
                String value = getString(jsonObject,key);  
                try{
	                Method method = pojoClass.getMethod("set" + key, String.class);
					method.invoke(pojo, value);
            	}catch(Exception e){
            		e.printStackTrace();
            	}
            } 
            formList.add(pojo);
		}
	} catch (Exception ex) {
		ex.printStackTrace();
		return null;
	}
	return formList;
}

@SuppressWarnings("unchecked")
public Object getPojoFromJSONObject(Class pojoClass, String json){
	if (json == null || json.equals("")) {
		return null;
	}
	JSONObject jsonObject = null;
	Object pojo = null;
	try {
		pojo = (Object) pojoClass.newInstance();
		jsonObject = new JSONObject(json);
		Iterator<?> it = jsonObject.keys();
		while(it.hasNext()){//遍历JSONObject  
        	String key = (String) it.next().toString();  
            String value = getString(jsonObject,key);  
            try{
                Method method = pojoClass.getMethod("set" + key, String.class);
				method.invoke(pojo, value);
            }catch(Exception e){
            	e.printStackTrace();
            }
        }
	} catch (Exception ex) {
		ex.printStackTrace();
		return null;
	}
	return pojo;
}

public String getData() {
	return data;
}

public void setData(String data) {
	this.data = data;
}



}